package com.bank.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bank.code.RespCode;
import com.bank.dao.TellerMapper;
import com.bank.entity.Teller;
import com.bank.exception.CommonException;
import com.bank.service.TellerService;
import com.bank.util.CommonUtil;
import com.bank.util.SessionUtils;

@Service(value="tellerService")
public class TellerServiceImpl implements TellerService {
	
	Logger logger = Logger.getLogger(TellerServiceImpl.class);
	
	@Autowired
	TellerMapper  tellerMapper;

	/**
	 * 查询柜员列表
	 * 
	 * @param  param
	 * @return List<Object>
	 */
	public List<Object> getTellerList(Map<String, Object> param) {
	
		List<Teller> tellerList=tellerMapper.findTellefList(param);
		List<Object> objectList = new ArrayList<Object>();
		for(Teller teller:tellerList){
			objectList.add(teller);
		}
		return objectList;
	}
	
	/**
	 *  
	 * 更新柜员信息
	 * @param param 输入参数
	 * @return void 
	 * @exception CommonException 柜员不存在
	 */
	public void updateTellerInfo(Map<String, Object> param) throws CommonException {
		String teller_name = (String)param.get("teller_name");
		String teller_role = (String)param.get("teller_role");
		String teller_status = (String)param.get("teller_status");
		String teller_phone = (String)param.get("teller_phone");
		String teller_email = (String)param.get("teller_email");
		String teller_id_type = (String)param.get("teller_id_type");
		Teller teller = this.getTellerByNumber((String)param.get("teller_number"));
		if(teller==null){
			throw new CommonException(RespCode.SYSTEM_FAILUE, "系统错误");
		}
		
		if(teller_name!= null && teller_name.equals("")==false){
			teller.setTeller_name(teller_name);
		}
		if(teller_role!=null && teller_role.equals("")== false){
			teller.setTeller_role(teller_role);
		}
		if(teller_status!=null && teller_status.equals("")==false){
			teller.setTeller_status(teller_status);
		}
		if(teller_phone!=null && teller_phone.equals("")==false){
			teller.setTeller_phone(teller_phone);
		}
		if(teller_email!=null && teller_email.equals("")==false){
			teller.setTeller_email(teller_email);
		}
		if(teller_id_type!=null && teller_id_type.equals("")==false){
			teller.setTeller_id_type(teller_id_type);
		}
		tellerMapper.updateByPrimaryKeySelective(teller);
		
	}
	/**
	 * 根据柜员代号查询柜员信息
	 * @param number柜员代号
	 * @return  获取的柜员信息
	 */
	public Teller getTellerByNumber(String number) {
		Teller teller = tellerMapper.selectByPrimaryKey(number);
		return  teller;
	}

	/**
	 * 增加柜员
	 * @param params
	 * @return
	 * 
	 */
	public void addTeller(Map<String, Object> params) throws CommonException {
		Teller teller = null;
		teller  = this.getTellerByNumber((String)params.get("teller_number"));
		if(teller != null){
			throw new CommonException(RespCode.TELLER_IS_EXIST, "柜员代号已存在");
		}
		teller = null;
		teller = new Teller();
		teller.setTeller_number((String)params.get("teller_number"));
		teller.setTeller_name((String)params.get("teller_name"));
		teller.setTeller_password(CommonUtil.passwordEncryption((String)params.get("teller_password")));
		teller.setTeller_role((String)params.get("teller_role"));
		teller.setTeller_status("0");
		teller.setTeller_phone((String)params.get("teller_phone"));
		teller.setTeller_email( (String)params.get("teller_email"));
		teller.setTeller_id_type((String)params.get("teller_id_type"));
		teller.setTeller_idno((String)params.get("teller_idno"));
		teller.setOrganization((String)params.get("organization"));
		tellerMapper.insertSelective(teller);
	}
	
	
	/**
	 * 根据柜员实体类直接更新柜员信息
	 * @param teller
	 * @throws CommonException
	 */
	public void updateTellerInfoByEntity(Teller teller) throws CommonException {
		Integer result = tellerMapper.updateByPrimaryKey(teller);
		if(result == 0){
			throw new CommonException();
		}
	}

	/** 
	 * 登录
	 * @param number 柜员代号
	 * @param password 请求类
	 *
	 */
	public void login(String number, String password, HttpServletRequest request) throws CommonException {
		// TODO Auto-generated method stub
		Teller teller = this.getTellerByNumber(number);
		
		if(teller == null){
			throw new CommonException(RespCode.USER_NOT_EXIST, "用户名不存在");
		}
		
		String passwordM = CommonUtil.passwordEncryption(password);
		//if(passwordM.equals(teller.getTeller_password())==false){
		if(false){
			throw new CommonException(RespCode.PASSWORD_NOT_CORRECT, "密码不正确");
		}else{
			SessionUtils.setSessionUserName(request, teller.getTeller_name());;
			SessionUtils.setSessionUserNumber(request, teller.getTeller_number());
			SessionUtils.setSessionUserOri(request, teller.getOrganization());
			teller.setTeller_status("1"); //设置登录状态，1已登录
			this.updateTellerInfoByEntity(teller);
			SessionUtils.setSesssionWorkdate(request);
		}

	}

	/**
	 * 退出
	 * @param request 
	 * @return
	 */
	public String logout(HttpServletRequest request) {
		String number = (String) SessionUtils.getSessionUserNumber(request);
		Teller teller = this.getTellerByNumber(number);
		teller.setTeller_status("0");
		try {
			this.updateTellerInfoByEntity(teller);
			SessionUtils.removeSessionUserName(request);
			SessionUtils.removeSessionUserNumber(request);
			SessionUtils.removeSessionWokedate(request);
			SessionUtils.removeSessoionUserOri(request);
			SessionUtils.removeSessionUserNumber(request);
		} catch (CommonException e) {
			e.printStackTrace();
		}
		return  "index";

	}

	
}
